package com.wewins.fota.sys;

import com.alibaba.fastjson.JSON;
import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.common.constant.AuthConst;
import com.wewins.fota.common.constant.HttpConst;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UnauthorizedEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        response.setStatus(HttpConst.STATUS_OK);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        JSON.writeJSONString(response.getWriter(), ResponseRst.builder()
                .status(HttpConst.STATUS_UNAUTHORIZED)
                .message(AuthConst.MSG_FAIL_UNAUTHORIZED).build()
        );
    }
}
